package com.larry.rabbitmq.third;

import com.larry.rabbitmq.util.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Delivery;

/**
 * @author larry
 * @Title: larry-rabbitmq
 * @Package com.larry.rabbitmq.third
 * @Description: 消费者
 * @date 6/28/2111:28 PM
 */
public class Work03 {

    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String args[]) throws Exception{
        Channel channel = RabbitMQUtils.getChannel();
        System.out.println("work03等待接收消息...时间短....");


        //设置不公平分发
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);

        //手动应答

        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME,autoAck,(consumerTag, message) -> {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("接收到的消息... " + new String(message.getBody()));
            //手动应答
            /**
             * 消息的标记 tag
             * 是否批量应答
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        },consumerTag -> {});
    }
}
